//身体评估智能输入界面
const plugin = requirePlugin("WechatSI")
const manager = plugin.getRecordRecognitionManager()
let backgroundAudioManager = wx.getBackgroundAudioManager()
import anaScore from '../../utils/anaScore.js'
import {setAddress} from '../../utils/settings'

Page({
    data: {
        type: "",
        isBegin: true,
        userInfo: {},
        questionList: [
            {
                question: "能独立完成仰卧、侧卧、俯卧的体位变化吗",
                img: "",
                type: "turnOverScore",
                title: "运动功能评估"
            },
            {
                question: "能保持独立坐5分钟吗",
                img: "../image/1155869.gif",
                type: "sitScore",
                title: "运动功能评估"
            },
            {
                question: "全脚掌着地站立1分钟能独立完成吗",
                img: "",
                type: "standScore",
                title: "运动功能评估"
            },
            {
                question: "在床、轮椅、椅子、便器等之间的移动能独立完成吗",
                img: "",
                type: "transferScore",
                title: "运动功能评估"
            },
            {
                question: "在平地连续走20单步或驱动轮椅50米能独立完成吗",
                img: "",
                type: "moveScore",
                title: "运动功能评估"
            },
            {
                question: "连续上下12级高约15厘米的台阶可以独立完成吗",
                img: "",
                type: "upAndDownStairsScore",
                title: "生活自理能力"
            },
            {
                question: "能独立将食物送入口中并完成进食吗",
                img: "../image/1243921.gif",
                type: "eatScore",
                title: "生活自理能力"
            },
            {
                question: "能独立穿脱衣物吗",
                img: "",
                type: "wearScore",
                title: "生活自理能力"
            },
            {
                question: "能独立完成洗脸、刷牙、梳头中的任意一项吗",
                img: "",
                type: "bathScore",
                title: "生活自理能力"
            },
            {
                question: "能独立使用便器并进行便后清洁吗",
                img: "",
                type: "toiletUsingScore",
                title: "社会适应能力"
            },
            {
                question: "在言语、手势、文字和图示上的理解与表达能力如何",
                img: "../image/1189674.gif",
                type: "communicationScore",
                title: "社会适应能力"
            },
            {
                question: "能独立从事日常家务劳动的任意一种吗",
                img: "",
                type: "houseworkScore",
                title: "社会适应能力"
            },
            {
                question: "上学、劳动或外出活动能独立完成吗",
                img: "",
                type: "activityScore",
                title: "社会适应能力"
            },
        ],
        answerList: ["能够独立完成", "需要大部分帮助", "需要小部分帮助", "完全依赖帮助", "请再说一次"],
        currentText: '',
        thisQuestion: '',
        thisQuestionFlag: 0,
        thisTitle: "",
        imgsrc: "",
        beginTag: 0,
        endTag: 12,
        showHintcard: false,
    },
    onShow() {
        this.setData({
            userInfo: wx.getStorageSync("userInfo")
        })
    },
    onLoad: function (options) {
        if (options.type == "运动功能") {
            this.setData({
                beginTag: 0,
                endTag: 4
            })
        }
        if (options.type == "生活自理") {
            this.setData({
                beginTag: 5,
                endTag: 8
            })
        }
        if (options.type == "社会适应") {
            this.setData({
                beginTag: 9,
                endTag: 12
            })
        }
        this.data.type = options.type
        this.setQuestion(this.data.beginTag)
        this.initRecord()
        backgroundAudioManager.onEnded(() => {
            this.streamRecord()
        })
    },
    //问题控制器
    setQuestion(ques) {
        this.setData({
            thisQuestion: this.data.questionList[ques].question,
            thisQuestionFlag: ques,
            imgsrc: this.data.questionList[ques].img,
            thisTitle: this.data.questionList[ques].title,
        })
    },
    //选择答案
    finish(event) {
        this.data.userInfo.score[this.data.questionList[this.data.thisQuestionFlag].type] = parseInt(event.target.dataset.score)
        if (this.data.thisQuestionFlag < this.data.endTag) {
            wx.showToast({
                title: this.data.answerList[3 - event.target.dataset.score],
            })
            this.toNext()
        } else {
            wx.request({
                url: setAddress,
                data: this.data.userInfo,
                method: "POST",
                success:(res)=>{
                    wx.setStorageSync("userInfo", this.data.userInfo)
                    wx.setStorageSync('changed', 1)
                    wx.navigateBack({
                        url: '../question/question',
                    })
                },
                fail:(err)=>{
                  wx.showToast({
                    title: '保存失败',
                    icon: 'error'
                  })
                  console.log(err)
                }
              })
            
        }

    },
    //选择上一个问题或下一个问题
    toLast() {
        this.data.currentText = ""
        if (this.data.thisQuestionFlag > this.data.beginTag)
            this.setQuestion(this.data.thisQuestionFlag - 1)
        else
            wx.showToast({
                title: '没有问题啦',
                icon: 'error',
            })
    },
    toNext() {
        this.data.currentText = ""
        if (this.data.thisQuestionFlag < this.data.endTag)
            this.setQuestion(this.data.thisQuestionFlag + 1)
        else
            wx.showToast({
                title: '没有问题啦',
                icon: 'error'
            })
    },
    //语音输入模块
    //文字转语音
    playsound: async function (con) {
        plugin.textToSpeech({
            lang: "zh_CN",
            tts: true,
            content: con,
            success: function (res) {
                backgroundAudioManager.src = res.filename
                backgroundAudioManager.title = con
            },
            fail: function (res) {
                console.log("fail tts", res)
            }
        })
    },

    //录音功能
    streamRecord: function () {
        manager.start({
            lang: 'zh_CN',
        })
        this.setData({
            showHintcard: true
        })
    },
    streamRecordEnd: async function () {
        manager.stop()
        this.setData({
            showHintcard: false
        })
        console.log("end:" + this.data.currentText)
        console.log(this.data)
        let ans = await anaScore(this.data.currentText)
        if (ans != -1) {
            this.data.userInfo.score[this.data.questionList[this.data.thisQuestionFlag].type] = parseInt(ans)
            this.toNext()
        }
        wx.showToast({
            title: this.data.answerList[3 - ans],
            icon: 'error'
        })
        console.log(ans)
    },
    //语音转文字
    initRecord: function () {
        //有新的识别内容返回，则会调用此事件
        manager.onRecognize = (res) => {
            let text = res.result
            this.setData({
                currentText: text
            })
        }
        manager.onStop = (res) => {
            let text = res.result
            this.setData({
                currentText: text
            })
        }
    },
})